<?php
class Admin_EnquireController extends Apps_Controller_Action_Admin {

    public function readAction() {
        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $select = $dbAdapter
                ->select()
                ->from('order', null)
                ->join('customer', 'customer.id = order.customer_id', null)
                ->join('product', 'product.id = order.product_id', null)
                ->where('order.status =? ', Model_DbTable_Customer::STATUS_INACTIVE)
                ->order('order.id DESC')
                ->columns(array(
                        'id' => 'order.id',
                        'message' => 'order.message',
                        'created_date' => 'order.created_date',
                        'customer_id' => 'customer.id',
                        'full_name' => 'customer.full_name',
                        'email'     => 'customer.email',
                        'phone'     => 'customer.phone',
                        'address'   => 'customer.address',
                        'description'   => 'customer.description',
                        'product_id' => 'product.id',
                        'product_name' => 'product.name',
                        'product_code' => 'product.code',
                        'product_price_sale' => 'product.price_sale',
                ));

        $grid = new Grid_Order_Enquire('grid-enquire');
        $grid->setCaption('List Enquire');
        $grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($select));
        $grid->setView(new OeZend_Widget_Grid_View());
        $grid->setDisablePaginator();
        $grid->deploy();
        $this->view->grid = $grid;
    }

    public function deleteAction() {
        $id = $this->_request->getParam('id');
        if(!$id) {
            $this->_helper->redirector('read');
        }
        $tbOrder = new Model_DbTable_Order();
        $tbOrder->safelyUpdate(array('status' => Model_DbTable_Order::STATUS_DELETE), array('id =?' => $id));
        $this->_helper->redirector('read');
    }

    public function updateAction() {
        $this->_helper->layout()->disableLayout();
        $id = $this->_request->getParam('id');
        $t = $this->_request->getParam('t');

        if(!$id) {
            $this->_redirect('read');
        }
        $f = new Admin_Form_UpdateEnquire();

        if($t == 'save') {
            $this->view->message = "Update Error";
            if($this->_request->isPost()) {
                if($f->isValid($this->_request->getParams())) {
                    $tbOrder = new Model_DbTable_Order();
                    $id = $tbOrder->safelyUpdate(array(
                            'order_status_id' => $f->getValue('order_status_id'),
                            'payment_type_id' => $f->getValue('payment_type_id'),
                            'price' => $f->getValue('price'),
                            'description' => $f->getValue('description'),
                            'status' => Model_DbTable_Order::STATUS_ACTIVE
                        ), array('id =?' => $id));
                    if($id) {
                        $this->view->message = "Update Sucessfully";
                        if($order = $tbOrder->fetchRow(array('id =?' => $id))) {
                            $tbCustomer = new Model_DbTable_Customer();
                            $tbCustomer->safelyUpdate(array('status' => Model_DbTable_Customer::STATUS_ACTIVE),
                                                      array('id =?' => $order['customer_id']));
                        }
                    }
                }
            }
            $this->render('update-enquire-message');
        } else {
            $f->getElement('id')->setValue($id);
            $this->view->form = $f;
        }
    }
}